package com.qa.restclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class RestClient {
    final static org.apache.log4j.Logger Log = Logger.getLogger(RestClient.class);

    public void get(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        int responseStatusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("response status code ==> " + responseStatusCode);

        String responseString = EntityUtils.toString(httpResponse.getEntity(), "utf8");
        JSONObject responseJson = JSON.parseObject(responseString);
        System.out.println("response json from api ==> " + responseJson);

        Header[] headerArray = httpResponse.getAllHeaders();
        HashMap<String, String> hm = new HashMap<>();
        for (Header header : headerArray) {
            hm.put(header.getName(), header.getValue());
        }
        System.out.println("response heaser ==> " + hm);
    }

    public CloseableHttpResponse get2(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpGet);
        return closeableHttpResponse;
    }

    public CloseableHttpResponse get3(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

        Log.info("开始发送get请求");
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        Log.info("发送请求成功，得到响应对象");
        return httpResponse;
    }

    public CloseableHttpResponse post(String url, String entityString, HashMap<String, String> headerMap) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(entityString));

        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        return httpResponse;
    }

    public CloseableHttpResponse put(String url, String entityString, HashMap<String, String> headerMap) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(new StringEntity(entityString));

        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpPut.addHeader(entry.getKey(), entry.getValue());
        }

        CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPut);
        return closeableHttpResponse;
    }
}
